// pages/userinfoedit/userinfoedit.js

var api = require('../../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userbaseinfo: api.getStorageData().userbaseinfo,
    wxuserinfo: api.getStorageData().wxUserInfo,
    editflag:'disabled',
    focus:false,
    avatar_file:'',
    avatar_path: '',
    avatar_src:'',
    hidden:true,
    avatar_src2:"sfsdf"
  },

  imgDefaultSrc:function(e){
    this.setData({
      avatar_src:'../../../assets/icon-defualt.png'
    })
  },
  exchange: function(){
    var that = this;
    api.post({
      url: 'Login/change',
      data: { token: that.data.userbaseinfo.token},
      success: function (data) {
        console.log(data);
        if (data.code == 200) {
          wx.showToast({
            title: data.data.message,
            icon: 'success'
          });
          data = data.data;
          var userbaseinfo = that.data.userbaseinfo;
          userbaseinfo.data.balance = data.data.balance; //余额
          userbaseinfo.data.score = data.data.score; //钻石
          
          wx.setStorage({
            key: "userbaseinfo",
            data: userbaseinfo,
            success: function (res) {
              that.setData({
                'userbaseinfo.data': userbaseinfo.data
              });
            },
            fail: function (res) {
              console.log(res);
            },
            complete: function (res) {
              console.log(res);
            }
          });

        }
      }
    });
  },

  avatarEdit:function(){
    var that = this;
    if (that.data.focus){
      wx.chooseImage({
        count: 1,
        success: function (res) {
          console.log(res.tempFilePaths[0]);
          var tempFilePaths = res.tempFilePaths;
          console.log(tempFilePaths);
          wx.uploadFile({
            url: 'https://cum.api.livestaring.com/v001/About/upload',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'token': that.data.userbaseinfo.token
            },
            success: function (res) {

              var data = JSON.parse(res.data);
              if (data.code == 200) {
                that.setData({
                  avatar_src: tempFilePaths[0],
                  avatar_file: data.data.file,
                  avatar_path: data.data.path
                });
              }

            },
            fail: function (res) {
              console.log(res);

            },
            complete: function (res) {
              console.log(res);
            }
          });
        },
      })
    }

  },

  editFun:function(){
    this.setData(
      {
        editflag: '',
        focus:true
      }
    );

  },

  editSubmit:function(res){
    this.setData(
      {
        editflag: 'disabled',
        focus: false
      }
    );
    var that = this;
    api.post({
      url: 'Login/edit',
      data: res.detail.value,
      success: function (data){
          console.log(data);
          if (data.code == 200) {
            wx.showToast({
              title: '修改成功',
              icon:'success'
            });
            data=data.data;
            var userbaseinfo = that.data.userbaseinfo;
            userbaseinfo.data.avatar = data.data.avatar;
            userbaseinfo.data.introduce = data.data.introduce;
            userbaseinfo.data.level = data.data.level;
            userbaseinfo.data.nickname = data.data.nickname;
            userbaseinfo.data.age = data.data.age;
            userbaseinfo.data.balance = data.data.balance; //余额
            userbaseinfo.data.score = data.data.score; //钻石
            userbaseinfo.data.mention = data.data.mention; //提现总额
            wx.setStorage({
              key: "userbaseinfo",
              data: userbaseinfo,
              success: function (res) {
                that.setData({
                  avatar_src: userbaseinfo.data.avatar,
                  'userbaseinfo.data': userbaseinfo.data
                });
                // 向上一页面传值
                var pages = getCurrentPages();
                if (pages.length > 1) {
                  //上一个页面实例对象
                  var prePage = pages[pages.length - 2];
                  //关键在这里
                  prePage.changeData(userbaseinfo)
                }

              },
              fail: function (res) {
                console.log(res);
              },
              complete: function (res) {
                console.log(res);
              }
            });

            



          }

      }
    });



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userbaseinfo: api.getStorageData().userbaseinfo,
      wxuserinfo: api.getStorageData().wxUserInfo,
      avatar_src: api.getStorageData().userbaseinfo.data.avatar
    });
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})